﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace SilverlightDesktop
{
    public partial class SilverlightWindowControl : UserControl
    {
        Point StartingDragPoint;

        public SilverlightWindowControl(int WindowSize)
        {
            // Required to initialize variables
            InitializeComponent();

            this.SetValue(WidthProperty, (double)(WindowSize));
            Window.SetValue(WidthProperty, (double)Window.Width - 400 + (WindowSize));
            DragStackPanel.SetValue(WidthProperty, (double)DragStackPanel.Width - 400 + (WindowSize));
            DragBar.SetValue(WidthProperty, (double)DragBar.Width - 400 + (WindowSize));
            TopSide.SetValue(WidthProperty, (double)TopSide.Width - 400 + (WindowSize));
            BottomSide.SetValue(WidthProperty, (double)BottomSide.Width - 400 + (WindowSize));

            Canvas.SetLeft(RightSide, (double)141.03 + (WindowSize));

            this.SetValue(HeightProperty, (double)(WindowSize));
            Window.SetValue(HeightProperty, (double)Window.Height - 400 + (WindowSize));
            LeftSide.SetValue(HeightProperty, (double)LeftSide.Height - 400 + (WindowSize));
            RightSide.SetValue(HeightProperty, (double)RightSide.Height - 400 + (WindowSize));

            Canvas.SetTop(BottomSide, (double)169.03 + (WindowSize));

            Loaded += new RoutedEventHandler(SilverlightWindowControl_Loaded);
        }

        void SilverlightWindowControl_Loaded(object sender, RoutedEventArgs e)
        {
            MoveToTop();
        }

        #region Close Button
        private void CloseButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.WindowControl.Visibility = Visibility.Collapsed;
        }
        #endregion

        #region DragBar
        private void DragBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MoveToTop();

            //Start Drag
            FrameworkElement DragBar = (FrameworkElement)sender;
            DragBar.CaptureMouse();

            // Set the starting point for the drag
            StartingDragPoint = e.GetPosition(DragBar);

            DragBar.MouseMove += new MouseEventHandler(DragBar_MouseMove);
            DragBar.MouseLeftButtonUp += new MouseButtonEventHandler(DragBar_MouseLeftButtonUp);
        }

        private void MoveToTop()
        {
            Canvas objCanvas = (Canvas)this.Parent;

            //Find the highest elemnt
            int intHighestElement = 0;
            foreach (UIElement UIElement in objCanvas.Children)
            {
                int intTmpHighestElement = Canvas.GetZIndex(UIElement);
                intHighestElement = (intTmpHighestElement > intHighestElement) ? intTmpHighestElement : intHighestElement;
            }

            //Canvas.SetZIndex(this, intHighestElement + 1);
            Canvas.SetZIndex(this, intHighestElement + 1);
        }

        private void DragBar_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //Stop Drag
            FrameworkElement DragBar = (FrameworkElement)sender;
            DragBar.ReleaseMouseCapture();

            DragBar.MouseMove -= new MouseEventHandler(DragBar_MouseMove);
            DragBar.MouseLeftButtonUp -= new MouseButtonEventHandler(DragBar_MouseLeftButtonUp);

        }

        private void DragBar_MouseMove(object sender, MouseEventArgs e)
        {
            Canvas Canvas = (Canvas)this.Parent;
            Point Point = e.GetPosition(Canvas);

            // Do not allow drag past the top or the left of the screen
            if (Point.Y < 0)
            {
                Point.Y = 0;
                return;
            }

            if (Point.X < 0)
            {
                Point.X = 0;
                return;
            }

            Canvas.SetLeft(this, Point.X - StartingDragPoint.X);
            Canvas.SetTop(this, Point.Y - StartingDragPoint.Y);
        }
        #endregion

        #region RightSide
        private void RightSide_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Start Drag
            FrameworkElement RightSide = (FrameworkElement)sender;
            RightSide.CaptureMouse();

            // Set the starting point for the drag
            StartingDragPoint = e.GetPosition(RightSide);

            RightSide.MouseMove += new MouseEventHandler(RightSide_MouseMove);
            RightSide.MouseLeftButtonUp += new MouseButtonEventHandler(RightSide_MouseLeftButtonUp);
        }

        void RightSide_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //Stop Drag
            FrameworkElement RightSide = (FrameworkElement)sender;
            RightSide.ReleaseMouseCapture();

            RightSide.MouseMove -= new MouseEventHandler(RightSide_MouseMove);
            RightSide.MouseLeftButtonUp -= new MouseButtonEventHandler(RightSide_MouseLeftButtonUp);
        }

        void RightSide_MouseMove(object sender, MouseEventArgs e)
        {
            Point Point = e.GetPosition(this);

            if (Point.X > (StartingDragPoint.X + 60))
            {
                this.SetValue(WidthProperty, (double)(Point.X - (StartingDragPoint.X)));
                Window.SetValue(WidthProperty, (double)(Point.X - (StartingDragPoint.X + 3)));
                DragBar.SetValue(WidthProperty, (double)(Point.X - (StartingDragPoint.X + 27)));
                DragStackPanel.SetValue(WidthProperty, (double)(Point.X - (StartingDragPoint.X + 1)));
                TopSide.SetValue(WidthProperty, (double)(Point.X - (StartingDragPoint.X - 3)));
                BottomSide.SetValue(WidthProperty, (double)(Point.X - (StartingDragPoint.X - 3)));
                Canvas.SetLeft(RightSide, Point.X - (StartingDragPoint.X));
            }
        }
        #endregion

        #region LeftSide
        private void LeftSide_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Start Drag
            FrameworkElement LeftSide = (FrameworkElement)sender;
            LeftSide.CaptureMouse();

            // Set the starting point for the drag
            StartingDragPoint = e.GetPosition(LeftSide);
            Point ControlPoint = e.GetPosition(this);
            StartingDragPoint.X = StartingDragPoint.Y - ControlPoint.Y - .5;

            LeftSide.MouseMove += new MouseEventHandler(LeftSide_MouseMove);
            LeftSide.MouseLeftButtonUp += new MouseButtonEventHandler(LeftSide_MouseLeftButtonUp);
        }

        void LeftSide_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //Stop Drag
            FrameworkElement LeftSide = (FrameworkElement)sender;
            LeftSide.ReleaseMouseCapture();

            LeftSide.MouseMove -= new MouseEventHandler(LeftSide_MouseMove);
            LeftSide.MouseLeftButtonUp -= new MouseButtonEventHandler(LeftSide_MouseLeftButtonUp);
        }

        void LeftSide_MouseMove(object sender, MouseEventArgs e)
        {
            Point LeftSidePoint = e.GetPosition(LeftSide);
            double PointDifference = LeftSidePoint.X - StartingDragPoint.X;

            if (PointDifference < 0)
            {
                PointDifference = PointDifference * -1;

                if (LeftSidePoint.X < (Window.Width + (PointDifference) - 60))
                {
                    this.SetValue(WidthProperty, (double)(PointDifference));
                    Window.SetValue(WidthProperty, (double)Window.Width + (PointDifference));
                    DragStackPanel.SetValue(WidthProperty, (double)DragStackPanel.Width + (PointDifference));
                    DragBar.SetValue(WidthProperty, (double)DragBar.Width + (PointDifference));
                    TopSide.SetValue(WidthProperty, (double)TopSide.Width + (PointDifference));
                    BottomSide.SetValue(WidthProperty, (double)BottomSide.Width + (PointDifference));

                    Canvas Canvas = (Canvas)this.Parent;
                    Point Point = e.GetPosition(Canvas);
                    Canvas.SetLeft(this, Point.X - StartingDragPoint.X);
                    Canvas.SetLeft(RightSide, (double)TopSide.Width + StartingDragPoint.X);
                }
            }
            else
            {
                if (LeftSidePoint.X < (Window.Width - (PointDifference) - 60))
                {
                    this.SetValue(WidthProperty, (double)(PointDifference));
                    Window.SetValue(WidthProperty, (double)Window.Width - (PointDifference));
                    DragStackPanel.SetValue(WidthProperty, (double)DragStackPanel.Width - (PointDifference));
                    DragBar.SetValue(WidthProperty, (double)DragBar.Width - (PointDifference));
                    TopSide.SetValue(WidthProperty, (double)TopSide.Width - (PointDifference));
                    BottomSide.SetValue(WidthProperty, (double)BottomSide.Width - (PointDifference));

                    Canvas Canvas = (Canvas)this.Parent;
                    Point Point = e.GetPosition(Canvas);
                    Canvas.SetLeft(this, Point.X - StartingDragPoint.X);
                    Canvas.SetLeft(RightSide, (double)TopSide.Width + StartingDragPoint.X);
                }
            }
        }
        #endregion

        #region BottomSide
        private void BottomSide_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Start Drag
            FrameworkElement BottomSide = (FrameworkElement)sender;
            BottomSide.CaptureMouse();

            // Set the starting point for the drag
            StartingDragPoint = e.GetPosition(BottomSide);

            BottomSide.MouseMove += new MouseEventHandler(BottomSide_MouseMove);
            BottomSide.MouseLeftButtonUp += new MouseButtonEventHandler(BottomSide_MouseLeftButtonUp);
        }

        void BottomSide_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //Stop Drag
            FrameworkElement BottomSide = (FrameworkElement)sender;
            BottomSide.ReleaseMouseCapture();

            BottomSide.MouseMove -= new MouseEventHandler(BottomSide_MouseMove);
            BottomSide.MouseLeftButtonUp -= new MouseButtonEventHandler(BottomSide_MouseLeftButtonUp);
        }

        void BottomSide_MouseMove(object sender, MouseEventArgs e)
        {
            Point Point = e.GetPosition(this);

            if (Point.Y > (StartingDragPoint.Y + 60))
            {
                this.SetValue(HeightProperty, (double)(Point.Y - (StartingDragPoint.Y)));
                Window.SetValue(HeightProperty, (double)(Point.Y - (StartingDragPoint.Y)) - 22);
                LeftSide.SetValue(HeightProperty, (double)(Point.Y - (StartingDragPoint.Y)));
                RightSide.SetValue(HeightProperty, (double)(Point.Y - (StartingDragPoint.Y)));
                Canvas.SetTop(BottomSide, Point.Y - (StartingDragPoint.Y));
            }
        }
        #endregion

        #region TopSide
        private void TopSide_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Start Drag
            FrameworkElement TopSide = (FrameworkElement)sender;
            TopSide.CaptureMouse();

            // Set the starting point for the drag
            StartingDragPoint = e.GetPosition(TopSide);
            Point ControlPoint = e.GetPosition(this);
            StartingDragPoint.Y = StartingDragPoint.X - ControlPoint.X + 1;

            TopSide.MouseMove += new MouseEventHandler(TopSide_MouseMove);
            TopSide.MouseLeftButtonUp += new MouseButtonEventHandler(TopSide_MouseLeftButtonUp);
        }

        void TopSide_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //Stop Drag
            FrameworkElement TopSide = (FrameworkElement)sender;
            TopSide.ReleaseMouseCapture();

            TopSide.MouseMove -= new MouseEventHandler(TopSide_MouseMove);
            TopSide.MouseLeftButtonUp -= new MouseButtonEventHandler(TopSide_MouseLeftButtonUp);
        }

        void TopSide_MouseMove(object sender, MouseEventArgs e)
        {
            Point TopSidePoint = e.GetPosition(TopSide);
            double PointDifference = TopSidePoint.Y - StartingDragPoint.Y;

            if (PointDifference < 0)
            {
                PointDifference = PointDifference * -1;

                if (TopSidePoint.X < (LeftSide.Height + (PointDifference) + 34))
                {
                    this.SetValue(HeightProperty, (double)(PointDifference));
                    Window.SetValue(HeightProperty, (double)Window.Height + (PointDifference));
                    LeftSide.SetValue(HeightProperty, (double)LeftSide.Height + (PointDifference));
                    RightSide.SetValue(HeightProperty, (double)RightSide.Height + (PointDifference));

                    Canvas Canvas = (Canvas)this.Parent;
                    Point Point = e.GetPosition(Canvas);
                    Canvas.SetTop(this, Point.Y - StartingDragPoint.Y);
                    Canvas.SetTop(BottomSide, (double)LeftSide.Height + StartingDragPoint.Y);
                }
            }
            else
            {
                if (TopSidePoint.X < (LeftSide.Height - (PointDifference) + 34))
                {
                    this.SetValue(HeightProperty, (double)(PointDifference));
                    Window.SetValue(HeightProperty, (double)Window.Height - (PointDifference));
                    LeftSide.SetValue(HeightProperty, (double)LeftSide.Height - (PointDifference));
                    RightSide.SetValue(HeightProperty, (double)RightSide.Height - (PointDifference));

                    Canvas Canvas = (Canvas)this.Parent;
                    Point Point = e.GetPosition(Canvas);
                    Canvas.SetTop(this, Point.Y - StartingDragPoint.Y);
                    Canvas.SetTop(BottomSide, (double)LeftSide.Height + StartingDragPoint.Y);
                }
            }

        }
        #endregion

        #region getBrushFromHexString
        public Brush getBrushFromHexString(string s)
        {
            // From: http://silverlight.net/forums/t/24190.aspx
            byte position = 0;
            string newColor = s.ToString();
            newColor = newColor.Replace("#", "");
            byte alpha = System.Convert.ToByte("ff", 16);

            if (newColor.Length == 8)
            {
                // get the alpha channel value
                alpha = System.Convert.ToByte(newColor.Substring(position, 2), 16);
                position = 2;
            }

            // get the red value
            byte red = System.Convert.ToByte(newColor.Substring(position, 2), 16);
            position += 2;

            // get the green value
            byte green = System.Convert.ToByte(newColor.Substring(position, 2), 16);
            position += 2;

            // get the blue value
            byte blue = System.Convert.ToByte(newColor.Substring(position, 2), 16);

            // create the SolidColorBrush object
            return new SolidColorBrush(Color.FromArgb(alpha, red, green, blue));
        } 
        #endregion

        #region DragBar_MouseEnter
        private void DragBar_MouseEnter(object sender, MouseEventArgs e)
        {
            this.DragBar.Fill = getBrushFromHexString("#FF566DBB");
            CloseButtonBackground.Fill = getBrushFromHexString("#FF566DBB");
        } 
        #endregion

        #region DragBar_MouseLeave
        private void DragBar_MouseLeave(object sender, MouseEventArgs e)
        {
            this.DragBar.Fill = getBrushFromHexString("#FFA4B3E6");
            CloseButtonBackground.Fill = getBrushFromHexString("#FFA4B3E6");
        } 
        #endregion

    }
}